package com.zhouzi.zhaoyang.util;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.zhouzi.zhaoyang.R;

public class InputPhoneDialog extends android.app.Dialog implements View.OnClickListener {

    private TextView phoneTv;
    private TextView tv0;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private TextView tv5;
    private TextView tv6;
    private TextView tv7;
    private TextView tv8;
    private TextView tv9;
    private TextView tvDot;
    private TextView tvDelete;

    private String phone;

    private PhoneInputListener listener;

    public InputPhoneDialog(Context context) {
        super(context, R.style.ActionSheetDialogStyle);
        initView(context);
    }

    public void setListener(PhoneInputListener listener) {
        this.listener = listener;
    }

    private void initView(Context context) {
        View view = View.inflate(context, R.layout.dialog_phone_layout, null);

        phoneTv = view.findViewById(R.id.phoneEdit);

        tv0 = view.findViewById(R.id.tv0);
        tv1 = view.findViewById(R.id.tv1);
        tv2 = view.findViewById(R.id.tv2);
        tv3 = view.findViewById(R.id.tv3);
        tv4 = view.findViewById(R.id.tv4);
        tv5 = view.findViewById(R.id.tv5);
        tv6 = view.findViewById(R.id.tv6);
        tv7 = view.findViewById(R.id.tv7);
        tv8 = view.findViewById(R.id.tv8);
        tv9 = view.findViewById(R.id.tv9);
        tvDot = view.findViewById(R.id.tvDot);
        tvDelete = view.findViewById(R.id.tvDelete);

        tv0.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        tv5.setOnClickListener(this);
        tv6.setOnClickListener(this);
        tv7.setOnClickListener(this);
        tv8.setOnClickListener(this);
        tv9.setOnClickListener(this);
        tv1.setOnClickListener(this);
        tvDot.setOnClickListener(this);
        tvDelete.setOnClickListener(this);

        view.findViewById(R.id.tvInput).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String result = phoneTv.getText().toString();
                if (result.equals("请输入金额")) {
                    return;
                }
                if (listener != null) {
                    listener.onPhoneInput(result);
                    dismiss();
                }
            }
        });

        view.findViewById(R.id.tvCancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        setContentView(view);
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = DisplayUtils.getScreenWidth(context) * 6 / 7;
            window.setAttributes(lp);
            window.setGravity(Gravity.CENTER);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv0:
                phoneTv.append("0");
                break;
            case R.id.tv1:
                phoneTv.append("1");
                break;
            case R.id.tv2:
                phoneTv.append("2");
                break;
            case R.id.tv3:
                phoneTv.append("3");
                break;
            case R.id.tv4:
                phoneTv.append("4");
                break;
            case R.id.tv5:
                phoneTv.append("5");
                break;
            case R.id.tv6:
                phoneTv.append("6");
                break;
            case R.id.tv7:
                phoneTv.append("7");
                break;
            case R.id.tv8:
                phoneTv.append("8");
                break;
            case R.id.tv9:
                phoneTv.append("9");
                break;
            case R.id.tvDot:
                phoneTv.append(".");
                break;
            case R.id.tvDelete:
                String result = phoneTv.getText().toString();
                if (result.equals("请输入金额")) {
                    return;
                }
                if (result.length() > 1) {
                    phoneTv.setText(result.substring(0, result.length() - 1));
                } else {
                    phoneTv.setText("请输入金额");
                }
                break;
        }
    }
}
